package com.bytz.modules.cms.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.constant.CommonConstant;
import com.bytz.modules.cms.common.model.CategoryDiscountSearchVo;
import com.bytz.modules.cms.product.entity.ProductDiscountCategory;
import com.bytz.modules.cms.product.entity.ProductDiscountCategoryLevel;
import com.bytz.modules.cms.product.mapper.ProductDiscountCategoryLevelMapper;
import com.bytz.modules.cms.product.model.ProductDiscountCategoryDetail;
import com.bytz.modules.cms.product.service.IProductDiscountCategoryLevelService;
import com.bytz.modules.cms.productLine.entity.ProductLineLevel;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 莱宝CMS—产品管理—产品优惠分类等级折扣表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ProductDiscountCategoryLevelServiceImpl extends ServiceImpl<ProductDiscountCategoryLevelMapper, ProductDiscountCategoryLevel> implements IProductDiscountCategoryLevelService {

    @Override
    public List<ProductDiscountCategoryLevel> getByKeys(List<CategoryDiscountSearchVo> keys) {
        if (keys == null) {
            return null;
        }

        Set<String> categoryIds = new HashSet<>(keys.size());
        Set<String> levelIds = new HashSet<>(keys.size());
        for (CategoryDiscountSearchVo key : keys) {
            categoryIds.add(key.getProductCategoryId());
            levelIds.add(key.getProductLineLevelId());
        }

        return lambdaQuery()
                .in(ProductDiscountCategoryLevel::getDiscountCategoryId, categoryIds)
                .in(ProductDiscountCategoryLevel::getProductLineLevelId, levelIds)
                .list();
    }

    @Override
    public List<ProductDiscountCategoryDetail> getByDiscountCategory(String id) {
        MPJLambdaWrapper<ProductDiscountCategoryLevel> wrapper = new MPJLambdaWrapper<>();
        return baseMapper.selectJoinList(ProductDiscountCategoryDetail.class, wrapper
                .select(ProductDiscountCategoryLevel::getProductLineLevelId,
                        ProductDiscountCategoryLevel::getProductLineLevelName,
                        ProductDiscountCategoryLevel::getDiscountRatio)
                .select(ProductLineLevel::getProductLineName)
                .selectAs(ProductLineLevel::getDiscountRatio, ProductDiscountCategoryDetail::getLevelDiscountRatio)
                .innerJoin(ProductLineLevel.class, ProductLineLevel::getId, ProductDiscountCategoryLevel::getProductLineLevelId)
                .eq(ProductLineLevel::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(ProductDiscountCategoryLevel::getDiscountCategoryId, id)
                .orderByAsc(ProductLineLevel::getProductLineName));
    }

    @Override
    public List<ProductDiscountCategoryDetail> getByProductLineLevel(String id) {
        MPJLambdaWrapper<ProductDiscountCategoryLevel> wrapper = new MPJLambdaWrapper<>();
        return baseMapper.selectJoinList(ProductDiscountCategoryDetail.class, wrapper
                .select(ProductDiscountCategoryLevel::getProductLineLevelId,
                        ProductDiscountCategoryLevel::getProductLineLevelName,
                        ProductDiscountCategoryLevel::getDiscountRatio)
                .select(ProductLineLevel::getProductLineName)
                .select(ProductDiscountCategory::getCategoryName)
                .selectAs(ProductLineLevel::getDiscountRatio, ProductDiscountCategoryDetail::getLevelDiscountRatio)
                .innerJoin(ProductLineLevel.class, ProductLineLevel::getId, ProductDiscountCategoryLevel::getProductLineLevelId)
                .innerJoin(ProductDiscountCategory.class, ProductDiscountCategory::getId, ProductDiscountCategoryLevel::getDiscountCategoryId)
                .eq(ProductLineLevel::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(ProductDiscountCategory::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(ProductDiscountCategoryLevel::getProductLineLevelId, id)
                .orderByAsc(ProductLineLevel::getProductLineName));
    }
}
